/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.machineryrental.converters;

import cz.muni.fi.pa165.machineryrental.dto.CustomerDTO;
import cz.muni.fi.pa165.machineryrental.entities.Customer;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author David Katuscak, 359273
 */
public class CustomerDTOConverter {

    /**
     * Converts Customer entity to CustomerDTO
     */
    public static CustomerDTO toDTO(Customer entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity can't be null.");
        }

        CustomerDTO dto = new CustomerDTO();
        return applyToDTO(dto, entity);
    }

    /**
     * Converts CustometDTO to Customer entity
     */
    public static Customer toEntity(CustomerDTO dto) {
        if (dto == null) {
            throw new IllegalArgumentException("dto can't be null.");
        }

        Customer entity = new Customer();
        entity.setId(dto.getId());
        entity.setFirstName(dto.getFirstName());
        entity.setLastName(dto.getLastName());
        entity.setEmail(dto.getEmail());
        entity.setRentals(dto.getRentals());
        entity.setPhone(dto.getPhone());
        entity.setAddress(dto.getAddress());
        entity.setCustomerType(dto.getCustomerType());
        entity.setCompanyNumber(dto.getCompanyNumber());
        entity.setVatNumber(dto.getVatNumber());

        return entity;
    }

    /**
     * Overwrites DTO with information in entity
     */
    public static CustomerDTO applyToDTO(CustomerDTO dto, Customer entity) {
        if (entity == null) {
            throw new IllegalArgumentException("Entity can't be null.");
        }

        if (dto == null) {
            throw new IllegalArgumentException("dto can't be null.");
        }

        dto.setId(entity.getId());
        dto.setFirstName(entity.getFirstName());
        dto.setLastName(entity.getLastName());
        dto.setEmail(entity.getEmail());
        dto.setRentals(entity.getRentals());
        dto.setPhone(entity.getPhone());
        dto.setAddress(entity.getAddress());
        dto.setCustomerType(entity.getCustomerType());
        dto.setCompanyNumber(entity.getCompanyNumber());
        dto.setVatNumber(entity.getVatNumber());

        return dto;
    }
	
	/**
	 * Converts list of customer entities to list of customer dtos.
	 * @param customers Customers to convert.
	 * @return List of DTOs according to given customers.
	 */
	public static List<CustomerDTO> toDTOList(List<Customer> customers) {
		if (customers == null || customers.isEmpty()) {
			return new ArrayList<CustomerDTO>();
		}
		
		List<CustomerDTO> result = new ArrayList<CustomerDTO>();
		for (Customer c: customers) {
			result.add(CustomerDTOConverter.toDTO(c));
		}
		
		return result;
	}
}
